import { ConfigEnv, loadEnv } from 'vite'
import  alias from './vite/alias'
import setupPlugins from './vite/plugins/index'
import { parseEvn } from './vite/util'
import { visualizer } from "rollup-plugin-visualizer";
// https://vitejs.dev/config/
export default ({command,mode}:ConfigEnv)=>{
  const isBuild  =command=='serve'
  const root=process.cwd()
  const env=parseEvn(loadEnv(mode,root))
 return{
  plugins: [...setupPlugins(false),visualizer()],
  resolve:{
    alias:alias 
  },

  build: {
    rollupOptions: {
      emptyOutDir: true,
      output: {
        manualChunks(id: string) {
          if (id.includes('node_modules')) {
            return id.toString().split('node_modules/')[1].split('/')[0].toString()
          }
        },
      },
    },
  },

//开发环境设置
  server: {
    proxy: {
      '/api': {
        target: env.VITE_MOCK_ENABLE,
        //跨域请求携带cookie
        changeOrigin: true,
        rewrite: (path: string) => path.replace(/^\/api/, ''),
      },
    },
  },
} 
} 
  

